package com.xn.shortlink.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.xn.shortlink.admin.common.convention.result.Result;
import com.xn.shortlink.admin.common.convention.result.Results;
import com.xn.shortlink.admin.dto.req.UserLoginReqDTO;
import com.xn.shortlink.admin.dto.req.UserRegisterReqDTO;
import com.xn.shortlink.admin.dto.req.UserUpdateReqDTO;
import com.xn.shortlink.admin.dto.resp.UserActualRespDTO;
import com.xn.shortlink.admin.dto.resp.UserLoginRespDTO;
import com.xn.shortlink.admin.dto.resp.UserRespDTO;
import com.xn.shortlink.admin.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
public class UserController {
    private final UserService userService;
    /**
     * 查询用户信息
     * @param username
     * @return
     */
    @GetMapping("/api/short-link/admin/v1/user/{username}")
    public Result<UserRespDTO> getUserByUsername(@PathVariable ("username")String username){
        return Results.success(userService.getUserByUsername(username));
    }

    /**
     * 无脱敏返回用户信息
     * @param username
     *
     *
     * BeanUtil.toBean 方法只是复制属性值，并不会触发 @JsonSerialize 注解指定的序列化器，所以 phone 属性的值不会被脱敏处理。
     * 这就有点类似VO封装
     * 因为原先的业务逻辑getuser返回回来的是USERRESPDTO的对象类型  使用ToBean直接赋值
     * @return
     */
    @GetMapping("/api/short-link/admin/v1/actual/user/{username}")
    public Result<UserActualRespDTO> getActualUserByUsername(@PathVariable("username") String username) {
        return Results.success(BeanUtil.toBean(userService.getUserByUsername(username), UserActualRespDTO.class));
    }

    @GetMapping("/api/short-link/admin/v1/user/has-username")
    public Result<Boolean> hasUsername(@RequestParam String username){
        return Results.success(userService.hasUserName(username));
    }
    @PostMapping("/api/short-link/admin/v1/user")
    public Result<Void> register(@RequestBody UserRegisterReqDTO requestParam) {
        userService.register(requestParam);
        return Results.success();
    }
    @PutMapping("/api/short-link/admin/v1/user")
    public Result<Void> update(@RequestBody UserUpdateReqDTO requestParam){
        userService.update(requestParam);
        return Results.success();
    }

    /**
     * 用户登录
     * @param
     * @return
     */
    @PostMapping("/api/short-link/v1/user/login")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO requestParam){
        return Results.success(userService.login(requestParam));
    }
    /**
     * 检查用户是否登录
     */
    @GetMapping("/api/short-link/v1/user/check-login")
    public Result<Boolean> checkLogin(@RequestParam("username") String username, @RequestParam("token") String token) {
        return Results.success(userService.checkLogin(username, token));
    }
    /**
     * 用户退出登录
     */
    @DeleteMapping("/api/short-link/v1/user/logout")
    public Result<Void> logout(@RequestParam("username") String username, @RequestParam("token") String token) {
        userService.logout(username, token);
        return Results.success();
    }
}
